/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.simba.utils.tree;

import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.parser.lexparser.Options;
import java.io.*;
import java.util.ArrayList;

/**
 *
 * @author ngonga
 */
public class StanfordParser {

    LexicalizedParser l = null;
    Options options;
    String option;
    ArrayList<String> fileList;
    String outputFolder;
    String ser;

    public StanfordParser(String op, String folder, String output, String serialization) {
        options = new Options();
        option = op;
        fileList = getAllFiles(folder);
        outputFolder = output;
        ser = serialization;
    }

    public StanfordParser(String op, ArrayList<String> fList, String output, String serialization) {
        options = new Options();
        option = op;
        fileList = fList;
        outputFolder = output;
        ser = serialization;
    }

    public void setOp(String op) {
        option = op;
    }

    public static Tree getDependencies(String input, String serialization) {
        Tree result = new Tree();
        try {
            File tempIn = File.createTempFile("ininin", "in");
            tempIn.deleteOnExit();
            File tempOut = File.createTempFile("outout", "out");
            tempOut.deleteOnExit();
            //write input to temp file
            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tempIn)));
            writer.println(input);
            writer.close();

            // run stanford
            StanfordParser s = new StanfordParser("typedDependencies",
                    tempIn.getAbsolutePath(), tempOut.getAbsolutePath(),
                    serialization);
            s.parse(tempIn.getAbsolutePath(), tempOut.getAbsolutePath());

            // transform results into a tree
            BufferedReader reader = new BufferedReader(new FileReader(tempOut.getAbsolutePath()));
            String line = reader.readLine();
            String tree = "";
            while (line != null) {
                tree = tree + line + "\n";
                line = reader.readLine();
            }
            result = DependencyReader.getDependencies(tree);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public void setOutputFolder(String _outputFolder) {
        outputFolder = _outputFolder;
    }

    public static ArrayList<String> getAllFiles(String folder) {
        ArrayList<String> results = new ArrayList<String>();
        String[] fileList;
        String location;
        if (!new File(folder).isDirectory()) {
            results.add(folder);
            return results;
        }
        try {
            ArrayList<String> queue = new ArrayList<String>();
            queue.add(folder);
            while (!queue.isEmpty()) {
                if (!(new File(queue.get(0)).isDirectory())) {
                    results.add((String) queue.get(0));
                    queue.remove(0);
                } else {
                    location = queue.get(0);
                    fileList = new File(location).list();
                    for (int i = 0; i < fileList.length; i++) {
                        queue.add(location + "/" + fileList[i]);
                    }
                    queue.remove(0);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }

    public void parse(String input, String output) {
        String args[] = new String[4];
        args[0] = "-outputFormat";
        args[1] = option;
        args[2] = ser;
        //args[3] = corpusPath;

        //redirect and parse
        try {
            PrintStream stream = new PrintStream(new BufferedOutputStream(new FileOutputStream(output)));
            System.setOut(stream);
            args[3] = input;
            LexicalizedParser.main(args);
            stream.close();
            System.out.println("Input file = " + input + ", outputFile = " + output);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.setOut(System.out);
        }
    }

    public static void main(String args[]) {
        // basic idea
        // write controller that launches this parser with each of the options on
        // different folders
        // question is. How do we get the folder compressed at the end
        // lanch 7zip batch
        String s = "SAP is really happy to receive this grant.";
        System.out.println(getDependencies(s, "D:/Work/Java/Aeon/resources/englishFactored.ser.gz"));
    }
}
